package eu.telecombretagne.rsm.gui.frames;

import javax.swing.ImageIcon;
import javax.swing.JInternalFrame;

import eu.telecombretagne.rsm.gui.NameConsts;
import eu.telecombretagne.rsm.gui.frames.panels.AutomatePanel;
import eu.telecombretagne.rsm.protocol.ABP.util.AutomataState;

import java.awt.event.*;
import java.awt.*;

public class AutomateInternalFrame extends JInternalFrame implements ActionListener {
	
	private static final long serialVersionUID = 4674597109528692444L;

	private static final int xOffset = 700, yOffset = 5;
	
	private AutomatePanel panel;
	
	/**
	 * The constructor of the AutomateInternalFrame.
	 */
	public AutomateInternalFrame(){
		super(NameConsts.AUTOMATEGRAPH,
				false,	//resizable
				true,	//closable
				false,	//maximizable
				true);	//iconifiable
		
		setSize(500,500);
		setFrameIcon(new ImageIcon(this.getClass().getResource(NameConsts.GFXAUTOMATE)));
		setBackground(Color.white);
		setLocation(xOffset, yOffset);
        
		GridBagLayout gridBagLayout = new GridBagLayout();
		this.getContentPane().setLayout(gridBagLayout);
		
		panel = new AutomatePanel(getWidth()-50, getHeight()-50, 
				Color.white, Color.red, Color.green, new Color(235, 235, 235), Color.DARK_GRAY);
		
		this.getContentPane().add(panel);
	}
	
	/**
	 * Sets the state of the automate receiver graph.
	 * 
	 * @param pAutomateState A state of the receiver to be set.
	 */
	public void setAutomateStateReceiver(String pAutomateState){
		if(pAutomateState.equals(AutomataState.REPOS0.toString())){
			panel.setReceiver(true, false, false, false);
		} else if(pAutomateState.equals(AutomataState.REPOS1.toString())){
			panel.setReceiver(false, true, false, false);
		} else if(pAutomateState.equals(AutomataState.ATTENTE_ACK0.toString())){
			panel.setReceiver(false, false, true, false);
		} else if(pAutomateState.equals(AutomataState.ATTENTE_ACK1.toString())){
			panel.setReceiver(false, false, false, true);
		}
		
		panel.repaint();
	}
	
	/**
	 * Sets the state of the automate emitter graph.
	 * @param pAutomateState A state of the emitter to be set.
	 */
	public void setAutomateStateEmitter(String pAutomateState){
		if(pAutomateState.equals(AutomataState.REPOS0.toString())){
			panel.setEmitter(true, false, false, false);
		} else if(pAutomateState.equals(AutomataState.REPOS1.toString())){
			panel.setEmitter(false, true, false, false);
		} else if(pAutomateState.equals(AutomataState.ATTENTE_ACK0.toString())){
			panel.setEmitter(false, false, true, false);
		} else if(pAutomateState.equals(AutomataState.ATTENTE_ACK1.toString())){
			panel.setEmitter(false, false, false, true);
		}
		
		panel.repaint();
	}
	
	@Override
	public void actionPerformed(ActionEvent actionEvent){
		// TODO Auto-generated method stub
		
	}
}